<?php


namespace app\admin\model;

use think\Db;

class CarveModel extends BaseModel
{
    protected $name='carve';

    protected $pk='id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public  function doAddData($data=[],$mw=[]){

        $this->startTrans();
        try {
            $carve_inst = [
                'order_id' => $mw['order_id'],
                'tomb_id' => $mw['tomb_id'],
                'customer_id' => $mw['customer_id'],
                'mb_id' => $data['data']['mb_id'],
                'type' => implode(",",$data['data']['type']),
                'sumprice' => $data['data']['sumprice'],
                'add_user' => cmf_get_current_admin_id()
            ];

            $carve_list = self::create($carve_inst);

            $carve_id = $carve_list['id'];
            // 是否勾选碑文制作
            if (in_array("insc",explode(',',$carve_inst['type']))){

                $data['insc']['position'] = implode(",",$data['insc']['position']);
                $data['insc']['pay_time'] = strtotime($data['insc']['pay_time']);
                $data['insc']['carve_id'] = $carve_id;
                Db::name("carve_insc")->insert($data['insc']);

            }
            // 是否勾选字体贴金
            if (in_array("gold",explode(',',$carve_inst['type']))){

                $data['gold']['pay_time'] = strtotime($data['gold']['pay_time']);
                $data['gold']['carve_id'] = $carve_id;
                Db::name("carve_gold")->insert($data['gold']);

            }
            // 是否勾选磁像制作
            if (in_array("image",explode(',',$carve_inst['type']))){

                $data['image']['pay_time'] = strtotime($data['image']['pay_time']);
                $data['image']['carve_id'] = $carve_id;
                Db::name("carve_image")->insert($data['image']);
            }

            // 往财务表插数据
            $pay_ins = [
                'order_id'=>$mw['order_id'],
                'other_id'=>$carve_id,
                'tomb_id'=>$mw['tomb_id'],
                'tomb_num'=>$mw['tomb_num'],
                'total_price'=>$data['data']['sumprice'],//应收金额
                'type'=>3,//售后
                'itype'=>6,//墓碑刻制
                'add_time'=>time(),//备注
                'status'=> 0,// 付款方式
                'pay_sn'=>OrderPayLogModel::getselfPayNum('add_time','pay_sn','JS-'),// 'DJ'.date('YmdHi'),
                'real_name'=>$mw['buyer_name'],//购墓人姓名
                'real_phone'=>$mw['buyer_phone'],//购墓人联系方式
                'customer_id'=>$mw['customer_id'],
                'handle'=>cmf_get_current_admin_id(),
            ];

            $result = Db::name('order_pay_log')->insertGetId($pay_ins);
            $result = $carve_id;
            $this->commit();
        }catch(\Exception $e){
            $this->error='操作失败';
            $result = false;
            $this->rollback();
        }
        return $result;
    }
}